/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.helpers.cargar;

import com.uca.sisbec.mapping.Becario;
import com.uca.sisbec.mapping.CarreraXBecario;
import com.uca.sisbec.mapping.CarreraXBecarioId;
import java.util.List;
import org.hibernate.Session;

/**
 *
 * @author Alex
 */
public class CargarDatosHelper {
    private Session session;

    public CargarDatosHelper(Session session) {
        this.session = session;
    }
    
    public void CargarDatosBecarioDB(Becario becario) {
        CarreraXBecario cxb = new CarreraXBecario();
        CarreraXBecarioId cxbId = new CarreraXBecarioId();
        cxbId.setCarnet(becario.getCarnet());
        cxbId.setCodCarrera(becario.getCodCarrera());
        cxbId.setIdUniv(becario.getUniversidad().getIdUniv());

        cxb.setId(cxbId);
        cxb.setEstado("A"); //Cargado
        cxb.setFechaInicio(GetFechaActual());

        session.save(becario);
        session.flush();

        session.save(cxb);
        session.flush();
    }
    
    public void CargarDatosBecariosDB(List<Becario> becariosList){
        
        for (int i = 0; i < becariosList.size(); i++) {
            
            CarreraXBecario cxb = new CarreraXBecario();
            CarreraXBecarioId cxbId = new CarreraXBecarioId();
            cxbId.setCarnet(becariosList.get(i).getCarnet());
            cxbId.setCodCarrera(becariosList.get(i).getCodCarrera());
            cxbId.setIdUniv(becariosList.get(i).getUniversidad().getIdUniv());
            
            cxb.setId(cxbId);
            cxb.setEstado("A");
            cxb.setFechaInicio(GetFechaActual());
            
            session.save(becariosList.get(i));
            session.flush();
            
            session.save(cxb);
            session.flush();
        }
        
    }
    
    private java.sql.Timestamp GetFechaActual(){
        long timeNow = java.util.Calendar.getInstance().getTimeInMillis();
        java.sql.Timestamp ts = new java.sql.Timestamp(timeNow);
        return ts;
    }
}
